﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace SharpDiary.Core.Controls
{
    public class ListBoxEx : ListBox
    {
        #region Fields & Properties

        private Font _selectedItemFont;
        private LinearGradientBrush _selectedItemBkBrush;
        private Pen _selectedItemBorderPenOuter = new Pen(Color.FromArgb(183, 231, 252));
        private Pen _selectedItemBorderPenInner = new Pen(Color.FromArgb(223, 242, 253), 1);

        #endregion

        #region Constructors

        public ListBoxEx()
        {
            DoubleBuffered = true;
            DrawMode = DrawMode.OwnerDrawVariable;
            ItemHeight = 30;

            _selectedItemFont = new Font(this.Font, FontStyle.Bold);
        }

        #endregion

        #region Methods

        protected override void OnFontChanged(EventArgs e)
        {
            base.OnFontChanged(e);

            _selectedItemFont = new Font(this.Font, FontStyle.Regular);
        }

        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            //base.OnDrawItem(e);

            if (e.Index == -1 || Items.Count == 0) { return; }

            e.DrawBackground();

            //e.Graphics.FillRectangle(
            if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
            {
                _selectedItemBkBrush = new LinearGradientBrush(e.Bounds, Color.FromArgb(233, 247, 254), Color.FromArgb(197, 233, 251), 90);
                e.Graphics.FillRectangle(_selectedItemBkBrush, e.Bounds);

                Rectangle innerRect = e.Bounds;
                innerRect.Height -= 4;
                innerRect.Width -= 4;
                innerRect.Offset(2, 2);

                Rectangle outerRect = e.Bounds;
                outerRect.Height -= 1;
                outerRect.Width -= 1;

                e.Graphics.DrawRectangle(_selectedItemBorderPenOuter, outerRect);
                e.Graphics.DrawRectangle(_selectedItemBorderPenInner, innerRect);
            }

            TextRenderer.DrawText(e.Graphics,
                Items[e.Index].ToString(),
                (((e.State & DrawItemState.Selected) == DrawItemState.Selected) ? _selectedItemFont : e.Font),
                e.Bounds,
                this.ForeColor,
                TextFormatFlags.VerticalCenter | TextFormatFlags.HorizontalCenter | TextFormatFlags.EndEllipsis);

        }

        protected override void OnMouseHover(EventArgs e)
        {
            base.OnMouseHover(e);

           
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // ListBoxEx
            // 
            this.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.ResumeLayout(false);

        }

        #endregion

    }
}
